@echo off
setlocal enabledelayedexpansion

REM NetTools Windows 构建脚本
set APP_NAME=nettools
set VERSION=1.0.0
set BUILD_DIR=build

REM 获取当前时间
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do (
    set BUILD_DATE=%%a-%%b-%%c
)
for /f "tokens=1-2 delims=: " %%a in ('time /t') do (
    set BUILD_TIME=%%a:%%b
)
set BUILD_TIMESTAMP=%BUILD_DATE% %BUILD_TIME%

REM 设置构建标志
set LDFLAGS=-ldflags "-X main.AppVersion=%VERSION% -X 'main.BuildTime=%BUILD_TIMESTAMP%'"

REM 检查命令行参数
if "%1"=="" goto help
if "%1"=="help" goto help
if "%1"=="all" goto all
if "%1"=="build" goto build
if "%1"=="server" goto server
if "%1"=="client" goto client
if "%1"=="clean" goto clean
if "%1"=="test" goto test
if "%1"=="build-all" goto build_all
if "%1"=="init" goto init
goto help

:help
echo NetTools Windows 构建脚本
echo.
echo 可用命令:
echo   build.bat all        - 清理并构建所有程序
echo   build.bat build      - 构建服务端和客户端
echo   build.bat server     - 只构建服务端
echo   build.bat client     - 只构建客户端
echo   build.bat test       - 运行测试
echo   build.bat clean      - 清理构建文件
echo   build.bat build-all  - 跨平台构建
echo   build.bat init       - 初始化项目目录
echo   build.bat help       - 显示此帮助信息
echo.
echo 版本: %VERSION%
echo Go版本:
go version
goto end

:all
call :clean
call :build
goto end

:build
call :server
call :client
goto end

:server
echo 正在构建服务端...
if not exist %BUILD_DIR% mkdir %BUILD_DIR%
cd server
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-server.exe .
if %ERRORLEVEL% EQU 0 (
    echo 服务端构建完成: %BUILD_DIR%\nettools-server.exe
) else (
    echo 服务端构建失败！
    cd ..
    goto error
)
cd ..
goto :eof

:client
echo 正在构建客户端...
if not exist %BUILD_DIR% mkdir %BUILD_DIR%
cd client
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-client.exe .
if %ERRORLEVEL% EQU 0 (
    echo 客户端构建完成: %BUILD_DIR%\nettools-client.exe
) else (
    echo 客户端构建失败！
    cd ..
    goto error
)
cd ..
goto :eof

:test
echo 运行测试...
go test -v ./...
goto end

:clean
echo 清理构建文件...
if exist %BUILD_DIR% rmdir /s /q %BUILD_DIR%
echo 清理完成
goto :eof

:build_all
call :clean
echo 正在进行跨平台构建...
if not exist %BUILD_DIR% mkdir %BUILD_DIR%

REM Windows 64位
echo 构建 Windows 64位版本...
cd server
set GOOS=windows
set GOARCH=amd64
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-server-windows-amd64.exe .
cd ..\client
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-client-windows-amd64.exe .
cd ..

REM Linux 64位
echo 构建 Linux 64位版本...
cd server
set GOOS=linux
set GOARCH=amd64
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-server-linux-amd64 .
cd ..\client
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-client-linux-amd64 .
cd ..

REM macOS 64位
echo 构建 macOS 64位版本...
cd server
set GOOS=darwin
set GOARCH=amd64
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-server-darwin-amd64 .
cd ..\client
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-client-darwin-amd64 .
cd ..

REM ARM64 (Apple Silicon)
echo 构建 macOS ARM64版本...
cd server
set GOOS=darwin
set GOARCH=arm64
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-server-darwin-arm64 .
cd ..\client
go build %LDFLAGS% -o ..\%BUILD_DIR%\nettools-client-darwin-arm64 .
cd ..

echo 跨平台构建完成！
dir %BUILD_DIR%
goto end

:init
echo 初始化项目目录...
if not exist logs mkdir logs
if not exist configs mkdir configs
if not exist build mkdir build
echo 项目初始化完成
goto end

:error
echo 构建过程中发生错误！
exit /b 1

:end
echo 完成 